// pages/order/order_detail.js
import http from '../../utils/http.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.globalData.imgDomain,
    order:{},
    orderCode:'',
    userId:"",
    isLogin:false,
    wxPhone:"",
    isBuy:false
  },
  refresh(){
    this.getOrderDetail(this.data.orderCode);
  },
  goback(){
    wx.navigateBack({
      delta:8
    })
  },
  callkf(){
    wx.makePhoneCall({
      phoneNumber: '4006633677'
    })
  },
  getOrderDetail(orderCode){
    let me=this;
    if (!orderCode){
      return;
    }
    http.request({
      url: 'order/detail/' + orderCode,
      loadTip:'正在加载...',
      success:res=>{
        res.goodsAmount = res.goodsAmount.toFixed(2);
        res.discount = res.discount.toFixed(2);
        me.setData({
          order:res,
          userId:wx.getStorageSync("userId"),
          isBuy:me.isBuy(res.children)
        });
      }
    });
  },
  isBuy(orderChilds){
    let me=this;
    if (!orderChilds){
      return false;
    }
    let userId = wx.getStorageSync("userId");
    if(!userId){
      return false;
    }
    let findMyOrder = orderChilds.find(item=>{
      return item.userId && item.userId == userId
    });
    return !findMyOrder?false:true;
  },
  getSmsCode(e) {
    let me = this;
    let phone = e.detail;
    if (phone) {
      me.setData({
        wxPhone: phone
      });
    }
    if (!me.data.wxPhone) {
      return;
    }
    http.request({
      url: 'user/smsCode/' + me.data.wxPhone,
      loadTip: '正在加载',
      method: 'get',
      success: res => {
        if (res) {
          wx.showToast({
            icon: 'success',
            title: '验证码已发送'
          })
        }
      }
    });
  },
  bindAccount(e) {
    let me = this;
    if (!e.detail.phone) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号',
      })
      return;
    }
    if (!e.detail.smscode) {
      wx.showToast({
        icon: 'none',
        title: '请输入短信验证码',
      })
      return;
    }
    http.request({
      url: 'user/register2',
      loadTip: '正在请求',
      data: {
        phone: e.detail.phone,
        smscode: e.detail.smscode,
        wxkey: app.getSessionKey(),
        sex: 1
      },
      success: res => {
        me.setData({
          isLogin: false,
          userId: res.userId
        });
        wx.setStorageSync("userId", res.userId);
        wx.setStorageSync("phone", res.userName);
        me.refresh();
      }
    });
  },
  getPhoneNumber(e) {
    let me = this;
    http.request({
      url: 'user/getPhoneNumberOfWx2',
      loadTip: '授权中',
      data: {
        encrypData: e.detail.encryptedData,
        iv: e.detail.iv,
        wxKey: app.getSessionKey()
      },
      success: res => {
        let phoneInfo = JSON.parse(res);
        me.setData({
          wxPhone: phoneInfo.phoneNumber
        });
      }
    });
  },
  join(){
    let me=this;
    if (!app.isBind()) {
      me.setData({
        isLogin: true
      });
      return;
    }else{
      me.buyOrder();
    }
  },
  buyOrder(){
    let me=this;
    let parentId = 0;
    if (me.data.order.parentId) {
      parentId = me.data.order.parentId;
    } else {
      parentId = me.data.order.orderId;
    }
    wx.navigateTo({
      url: '../order/addorder?isGroup=true&goodsAttrId=' + me.data.order.orderGoods[0].goodsAttrId + '&parentId=' + parentId
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderCode: options.orderCode
    });
    this.getOrderDetail(options.orderCode);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '订单详情',
    })
    this.init();
  },

  init(){
    let me=this;
    if (!app.isBind()) {
      me.setData({
        isLogin: true
      });
      return;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let me=this;
    let imgPath = me.data.imgDomain + me.data.order.orderGoods[0].goodsThumb;
    let parentId = 0;
    if(me.data.order.parentId){
      parentId = me.data.order.parentId;
    }else{
      parentId = me.data.order.orderId;
    }
    return {
      title: '遇见你，方知味。一起来拼团',
      imageUrl: imgPath,
      path: '/pages/order/order_detail?orderCode=' + me.data.orderCode
    }
  }
})